<template>
  <div class="qualification-audit-container">
    <div class="header-actions">
      <h2>供应商提交资质审核</h2>
    </div>

    <div class="search-bar">
      <el-button type="primary" @click="fetchQualifications" style="margin-left: 10px;">刷新列表</el-button>
    </div>

    <div class="table-container">
      <el-table
        :data="qualificationList"
        style="width: 100% !important; height: 100% !important;"
        border
        stripe
        :header-cell-style="{ 'background': 'linear-gradient(90deg, #3b82f6 0%, #60a5fa 100%)', 'color': 'white', 'font-weight': 'bold', 'border-radius': '4px 4px 0 0' }"
        :row-class-name="tableRowClassName"
        :row-style="{ 'transition': 'background-color 0.2s' }"
        :cell-style="{ 'padding': '12px', 'border-radius': '4px' }"
        fit
        v-loading="loading"
        @row-mouse-enter="handleRowMouseEnter"
        @row-mouse-leave="handleRowMouseLeave"
      >
        <el-table-column prop="submissionId" label="提交ID" width="180"></el-table-column>
        <el-table-column prop="supplierId" label="供应商ID" width="100"></el-table-column>
        <el-table-column prop="businessLicense" label="营业执照" min-width="150" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.businessLicense && scope.row.businessLicense.trim()"
              :src="scope.row.businessLicense.trim()"
              :preview-src-list="[scope.row.businessLicense.trim()]"
              style="width: 40px; height: 40px; border-radius: 4px; cursor: pointer"
            ></el-image>
            <span v-else>未上传</span>
          </template>
        </el-table-column>
        <el-table-column prop="authorization" label="授权书" min-width="100" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="certificates" label="证书信息" min-width="120" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="submissionDate" label="提交日期" min-width="180" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="status" label="审核状态" min-width="120" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 'pending'" type="warning">待审核</el-tag>
            <el-tag v-if="scope.row.status === 'approved'" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status === 'rejected'" type="danger">审核拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="200" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="handleApprove(scope.row)">审核通过</el-button>
            <el-button type="danger" size="small" @click="handleReject(scope.row)">审核拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 审核拒绝对话框 -->
      <el-dialog title="审核拒绝" :visible.sync="rejectDialogVisible" width="500px">
        <el-form :model="form">
          <el-form-item label="拒绝理由" prop="reason">
            <el-input type="textarea" v-model="form.reason" rows="4" placeholder="请输入拒绝理由"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="rejectDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmReject">确定</el-button>
        </div>
      </el-dialog>
    </div>

    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        :pager-count="5"
      ></el-pagination>
    </div>


  </div>
</template>

<script>
import { getQualificationList, submitQualificationAudit } from '../api/qualificationApi';

export default {
  name: 'QualificationAudit',
  data() {
    return {
      qualificationList: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      rejectDialogVisible: false,
        form: {
          reason: ''
        },
        currentRow: null
    };
  },
  mounted() {
    this.fetchQualifications();
  },
  methods: {
    fetchQualifications() {
      this.loading = true;
      getQualificationList(this.pageNum, this.pageSize)
        .then(response => {
          console.log('response:', response);
          // 添加空值检查防止null引用错误
          if (response && response.data) {
            this.qualificationList = response.data.data.records || [];
            console.log('资质列表:', this.qualificationList);
            this.total = response.data.data.total || 0;
            this.loading = false;
          } else {
            this.qualificationList = [];
            this.total = 0;
            this.loading = false;
            this.$message.warning(response.msg);
          }
        })
        .catch(error => {
          console.error('获取资质列表失败:', error);
          this.loading = false;
          this.$message.error('获取资质列表失败');
        });
    },
    resetSearch() {
      this.pageNum = 1;
      this.fetchQualifications();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchQualifications();
    },
    handleCurrentChange(current) {
      this.pageNum = current;
      this.fetchQualifications();
    },
    
    tableRowClassName() {
      return 'hover-row';
    },
    handleRowMouseEnter(_, event) {
      event.currentTarget.style.backgroundColor = '#f0f7ff';
    },
    handleRowMouseLeave(_, event) {
      event.currentTarget.style.backgroundColor = '';
    },
    handleApprove(row) {
      console.log('Approve:', row);
      this.$confirm('确定要审核通过该记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true;
        submitQualificationAudit({
        submissionId: row.submissionId,
        status: 'approved',
        auditComments: ''
      }).then((response) => {
        if (response.data.code === 0) {
          this.$message.success('审核通过成功');
          this.fetchQualifications();
        }else {
          this.$message.error(response.response?.data?.msg || '审核失败');
          console.error(response);
        }
        }).catch(error => {
          this.$message.error(error.response?.data?.msg || '审核失败');
          console.error(error);
        }).finally(() => {
          this.loading = false;
        });
      });
    },
    handleReject(row) {
      this.currentRow = row;
      this.rejectReason = '';
      this.rejectDialogVisible = true;
    },
    confirmReject() {
      if (!this.form.reason.trim()) {
        this.$message.warning('请输入拒绝理由');
        return;
      }
      this.loading = true;
      submitQualificationAudit({
        submissionId: this.currentRow.submissionId,
        status: 'rejected',
        auditComments: this.form.reason
      }).then(() => {
        this.$message.success('审核拒绝成功');
        this.rejectDialogVisible = false;
        this.fetchQualifications();
      }).catch(error => {
        this.$message.error(error.response?.data?.msg || '审核失败');
        console.error(error);
      }).finally(() => {
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped>
.qualification-audit-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.header-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-bar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 15px;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.table-container {
  background-color: white;
  border-radius: 4px;
  padding: 15px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  background-color: white;
  padding: 15px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.hover-row {
  transition: background-color 0.2s;
}
</style>